<?php


namespace App\Http\Controllers\Warehouse;


use App\Http\Controllers\Controller;
use App\Http\Requests\Warehouse\Package\StoreRequest;
use App\Http\Requests\Warehouse\Package\UpdateRequest;
use App\Http\Searchs\Warehouse\PackageSearch;
use App\Repositories\Package;

class PackageController extends Controller
{
    /**
     * @var Package
     * @author luffyzhao@vip.126.com
     */
    private $package;

    /**
     * PackageController constructor.
     * @param Package $package
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Package $package)
    {
        $this->package = $package;
    }

    /**
     * @param PackageSearch $search
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function index(PackageSearch $search)
    {
        return $this->response(
            $this->package->paginate($search->toArray())
        );
    }

    /**
     * @param StoreRequest $request
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function store(StoreRequest $request){
        return $this->response(
            $this->package->create(
                $request->only(['name', 'price', 'weight', 'remark', 'length', 'width', 'height', 'bearing'])
            )
        );
    }

    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function edit($id){
        return $this->response(
            $this->package->find(
                $id
            )
        );
    }

    /**
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update(UpdateRequest $request, $id){
        return $this->response(
            $this->package->update($id, $request->only(['name', 'price', 'weight', 'remark', 'length', 'width', 'height', 'bearing']))
        );
    }


    /**
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     * @author luffyzhao@vip.126.com
     */
    public function show($id){
        return $this->response(
            $this->package->show($id)
        );
    }
}